import {Component, OnInit} from '@angular/core';
import {AddConZoneComponent} from '../add-con-zone.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {ConZone} from '../../con-zone.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';

@Component({
    selector: 'app-main-add-con-zone',
    templateUrl: './main-add-con-zone.component.html',
    styleUrls: ['./main-add-con-zone.component.css']
})
export class MainAddConZoneComponent implements OnInit {

    errRes = new ErrRes();

    domain = new ConZone();

    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
            file2: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file2.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file2.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serImg1 = this.extend.domain.file2.oss.fileUrl;
                        } else {
                            this.extend.domain.file2.oss.fileUrl = '';
                            this.domain.serImg1 = this.extend.domain.file2.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            file1: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file1.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file1.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serImg2 = this.extend.domain.file1.oss.fileUrl;
                        } else {
                            this.extend.domain.file1.oss.fileUrl = '';
                            this.domain.serImg2 = this.extend.domain.file1.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            }
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.conZone.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.conZone.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addConZone: AddConZoneComponent) {
        this.addConZone.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
    }

}
